<?php
/**
 * *
 *  * ============================================================================
 *  * Created by PhpStorm.
 *  * User: Ice
 *  * 邮箱: ice@sbing.vip
 *  * 网址: https://sbing.vip
 *  * Date: 2019/9/19 下午3:33
 *  * ============================================================================.
 */

namespace app\common\controller;

use think\facade\Config;
use think\facade\Event;

/**
 * 公共接口.
 */
class Ding
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    //内部钉钉通知
    public  function dingSend($title,$content=''){
        // $DingApi = new DingApi();
        $webhookurl = 'https://oapi.dingtalk.com/robot/send?access_token=8d2072035e0dc163796dfaaec73659f127000c62ae6cef64331b9523c7253f89';
		$data = array ('msgtype' => 'markdown','markdown' => array ('title' => $title,'text'=>$content.'@15511329132'),'at'=>array('atMobiles'=>array("15511329132"),"isAtAll" => false));
		$data_string = json_encode($data);
		// dump($webhookurl,$data_string);
		$result = $this->request_by_curl($webhookurl, $data_string);  
		
		return $result;
        // $res = $DingApi->sendMarkdown($webhookurl,$content,$title);
        // return $res;
    }
	//公共代码——curl请求
	// public  function request_by_curl($remote_server, $post_string) {  
	//     $ch = curl_init();  
	//     curl_setopt($ch, CURLOPT_URL, $remote_server);
	//     curl_setopt($ch, CURLOPT_POST, 1); 
	//     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
	//     curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
	//     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
	//     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
	//     // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
	//     curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
	//     curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
	//     $data = curl_exec($ch);
	//     curl_close($ch);                
	//     return $data;  
	// }
	public function request_by_curl($remote_server, $post_string) {  
	    $ch = curl_init();  
	    curl_setopt($ch, CURLOPT_URL, $remote_server);
	    curl_setopt($ch, CURLOPT_POST, 1); 
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
	    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
	    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
	    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
	    // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
	    // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
	    $data = curl_exec($ch);
	    curl_close($ch);                
	    return $data;  
	} 
}
